import { Injectable, Inject } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/internal/operators';
import { Singer, SingerDetail } from './data-types/common.type';
import { API_CONFIG, ServicesModule } from './services.module';

interface SingerParams {
  offset: number;
  limit: number;
  cat?: string;
}

const defaultParams: SingerParams = {
  offset: 0,
  limit: 9,
  cat: '5001'
};

@Injectable({
  providedIn: ServicesModule
})
export class SingerService {
  constructor(private http: HttpClient, @Inject(API_CONFIG) private url: string) { }
  getEnteredSinger(args: SingerParams = defaultParams): Observable<Singer[]> {
    const params = Object.assign(args);
    return this.http.get(this.url + '/artist/list', {params})
      .pipe(map((res: { artists: Singer[]}) => res.artists));
  }
  // 歌手详细信息
  getSingerDetail(id: string): Observable<SingerDetail> {
    const params = new HttpParams().set('id', id);
    return this.http.get(this.url + '/artists', {params})
      .pipe(map((res: SingerDetail) => res));
  }
  // 相似歌手列表
  getSimiSinger(id: string): Observable<Singer[]> {
    const params = new HttpParams().set('id', id);
    return this.http.get(this.url + '/simi/artist', {params})
      .pipe(map((res: { artists: Singer[]}) => res.artists));
  }
}
